<template>
  <div id="home">
    <!--导航栏-->
    <nav-bar class="nav-bar">
      <div slot="center">购物街</div>
    </nav-bar>

    <tab-control @tabClick="tabClick" :titles="tabControl" ref="tabControl1" class="tab-control"
                 v-show="isTabFixed"/>


    <scroll class="content" ref="scroll"
             :pull-up-load="true"
            @pullingUp="loadMore" @scroll="cententScroll">
      <!--轮播图-->
      <home-swiper @SwiperImageLoad="SwiperImageLoad" :banner="banner"/>
      <!--推荐栏-->
      <recommend-view :recommend="recommend"/>
      <!--本周推荐-->
      <feature-view></feature-view>
      <!--推荐-->
      <tab-control @tabClick="tabClick" :titles="tabControl" ref="tabControl2"/>
      <!--商品内容-->
      <goods-list :goods="showGoods"/>

    </scroll>

    <!--回到顶部-->
    <back-top v-show="isShowBackTop" @click.native="backTop"/>
  </div>
</template>

<script>
  import HomeSwiper from "./childComps/HomeSwiper";
  import RecommendView from "./childComps/RecommendView";
  import FeatureView from "./childComps/FeatureView";

  import NavBar from "components/common/navbar/NavBar";
  import Scroll from "../../components/common/scroll/Scroll";

  import TabControl from "../../components/content/tabControl/TabControl";
  import GoodsList from "../../components/content/goods/GoodsList";
  import BackTop from "../../components/content/backTop/BackTop";

  import {getHomeMultiData, getHomeGoods} from "network/home";
  import {ShowBackTOPYAxis} from "../../common/constant";


  export default {
    name: "Home",
    data() {
      return {
        banner: [],
        recommend: [],
        tabControl: ['流行', '新款', '精选'],
        goods: {
          'pop': {page: 0, list: []},
          'new': {page: 0, list: []},
          'sell': {page: 0, list: []}
        },
        currentType: "pop",
        isShowBackTop: false, /*是否隐藏 回到顶部图标*/
        tabOffsetTop: 546,
        isTabFixed: false, // 是否吸顶
        saveY: 0
      }
    },
    created() {
      //请求多个数据
      this.getHomeMultiData()

      // 请求商品数据
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')

    },
    activated() {
      this.$refs.scroll.scrollTo(0, this.saveY, 100) /*添加100毫秒*/
      this.$refs.scroll.scroll.refresh()

    },
    deactivated() {
      this.saveY = this.$refs.scroll.scroll.y

    },
    computed: {
      showGoods() { // 获取到当前点击的类型数据
        return this.goods[this.currentType].list
      }
    },
    methods: {
      /*
      * 事件监听
      * */
      tabClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop'
            break
          case 1:
            this.currentType = 'new'
            break
          case 2:
            this.currentType = 'sell'
            break
        }
        this.$refs.tabControl1.currentIndex = index
        this.$refs.tabControl2.currentIndex = index
      },
      /*返回顶部*/
      backTop() {
        this.$refs.scroll.scrollTo(0, 0, 1000)
      },
      /*监听滚动*/
      /*当 位置草超过1000 , 显示 返回顶部*/
      cententScroll(position) {
        //1.判断BackTOp 是否显示
        this.isShowBackTop = -position.y > ShowBackTOPYAxis

        //2.tab-control是否吸顶(position:fixed)
        this.isTabFixed = (-position.y > this.tabOffsetTop)

      },
      /*监听是否到达最底部,加载数据*/
      loadMore() {
        this.getHomeGoods(this.currentType)
      },
      /*监听轮播图的图片是否加载完成*/
      SwiperImageLoad() {

        this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
      },
      /*
      * 网络请求
      * */
      getHomeMultiData() {
        getHomeMultiData().then(res => {
          this.banner = res.data.banner.list // 轮播图数据
          this.recommend = res.data.recommend.list // 推荐栏
        })
      },
      getHomeGoods(type) {
        const page = this.goods[type].page + 1 // 获取当前页码+1
        getHomeGoods(type, page).then(res => {
          if (res === "") {
            console.log("无数据")
            return
          }

          this.goods[type].list.push(...res.data.list) // 将数据追加进去
          this.goods[type].page = page // 请求成功时,将页码修改

        })
      }
    },
    components: {
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
    }
  }
</script>

<style scoped>
  #home {
    height: 100vh;
    position: relative;
  }

  .nav-bar {
    background-color: var(--color-tint);
    color: var(--color-background);

  }

  .content {
    overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
  }

  .tab-control {
    position: relative;
    z-index: 100;
  }
</style>
